<template>
	<div>
		<div style="float: right;margin-right:2%;">
			<el-button style='margin: 10px 0;' :disabled="!isAuth('travelNotes:add')" size="mini" type="primary"
				icon="document" @click="addNotice">添加</el-button>
		</div>
		<el-table v-loading="tableDataLoading" :data="tableData.records">

			<el-table-column fixed prop="id" label="编号" width="100">
			</el-table-column>
			<el-table-column prop="title" label="备注标题">
			</el-table-column>
			<el-table-column prop="content" label="备注值">
			</el-table-column>
			<!-- <el-table-column prop="isEnable" label="是否启用">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.isEnable" @change="change2(scope.row.isEnable,scope.row)"
						:active-value="openValue" :inactive-value="closeValue" active-color="#13ce66"
						inactive-color="#ff4949">
					</el-switch>
				</template>
			</el-table-column> -->
			<el-table-column label="操作" width="180">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" :disabled="!isAuth('travelNotes:update')"
						@click="amendBanner(scope.row)">修改
					</el-button>
					<el-button size="mini" type="danger" :disabled="!isAuth('travelNotes:delete')"
						@click="deletes(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="text-align: center;margin-top: 10px;">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-sizes="[10, 20, 30, 50, 100]" :page-size="limit" :current-page="page"
				layout="total,sizes, prev, pager, next,jumper" :total="tableData.total">
			</el-pagination>
		</div>
		<el-dialog :title="title" :visible.sync="dialogFormVisible" center>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">备注标题：</span>
				<el-input style="width:50%;" v-model="rewardProp" placeholder="请输入备注标题"></el-input>
			</div>
			<div style="margin-bottom: 10px;">
				<span style="width: 200px;display: inline-block;text-align: right;">备注值：</span>
				<span class="bqList" v-for="(item,index) in labels" :key="index">{{item}}
					<span class="delss">
						<i class="el-icon-delete" @click="dels1(index)"></i>
					</span>
				</span>
				<el-input v-model="bq" style="width:200px;" placeholder="请输入标签"
					 @keydown.enter.native="btnTj"></el-input>
				<!-- <button class="tj" @click="btnTj">添加标签</button> -->
				<el-button type="primary" class="tj" size="mini" icon="el-icon-edit" circle @click="btnTj">
				</el-button>
			</div>
			
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="addNoticeTo()">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				limit: 10,
				tableDataLoading: true,
				dialogFormVisible: false,
				tableData: [],
				num: '',
				memberImg: '',
				isEnable: 1,
				rewardProp: '',
				redPacketNum: '',
				sort: '',
				rewardId: '',
				title: '添加',
				openValue: 1,
				closeValue: 0,
				labels:[],
				bq:'',
			}
		},
		methods: {
			// 获取数据列表
			dataSelect() {
				this.tableDataLoading = true
				this.$http({ 
					url: this.$http.adornUrl(`message/selectMessageByType`),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.page,
						'limit': this.limit,
						'state': 10
					})
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						console.log('会员列表', data)
						this.tableDataLoading = false
						let returnData = data.data;
						this.tableData = returnData;
						if(this.tableData.records.length==0){
							if(this.page>1){
								this.page = this.page-1
								this.dataSelect()
							}
						}
					}

				})
			},
			//上传成功
			handleAvatarSuccess(file) {
				this.memberImg = file.data
			},
			addNotice() {
				this.rewardProp = ''
				this.rewardId = ''
				this.labels = []
				this.bq = ''
				this.title = '添加'
				this.dialogFormVisible = true
			},
			handleCurrentChange(val) {
				this.page = val
				this.dataSelect()
			},
			handleSizeChange(val) {
				this.limit = val
				this.dataSelect()
			},
			// 添加banner图
			addNoticeTo() {
				if (this.rewardProp == '') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入分成比例',
						type: 'warning'
					});
					return
				}
				if (this.labels.length == 0) {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请添加备注值',
						type: 'warning'
					});
					return
				}
				this.classify = 1
				if (this.title == '添加') {
					var url = 'message/add'
				} else {
					var url = 'message/update'
				}
				this.$http({
					url: this.$http.adornUrl(url),
					method: 'post',
					// params: this.$http.adornParams({
					data: this.$http.adornData({
						'title': this.rewardProp,
						'content': this.labels.toString(),
						'id': this.rewardId,
						'state':10,
					})
				}).then(({
					data
				}) => {
					if(data.code==0){
						this.dialogFormVisible = false
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								
								this.dataSelect()
							}
						})
					}else{
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {
							}
						})
					}
					
				})
			},
			// 修改
			amendBanner(rows) {
				
				this.dialogFormVisible = true
				this.rewardId = rows.id
				this.rewardProp = rows.title
				if(rows.content){
					this.labels = rows.content.split(',')
				}else{
					this.labels =[]
				}
				
				this.title = '修改'
			},
			//删除一级
			deletes(row) {
				let delid = row.id
				this.$confirm(`确定删除此条信息?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: ''
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl('message/delete/'+delid),
						method: 'get',
						params: this.$http.adornParams({
							// 'rewardId':delid
						})
					}).then(({
						data
					}) => {
						if(data.code==0){
							this.$message({
								message: '删除成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.dataSelect()
								}
							})
						}else{
							this.$message({
								message: data.msg,
								type: 'warning',
								duration: 1500,
								onClose: () => {
								}
							})
						}
						
					})
				}).catch(() => {})
			},
			// 是否启用
			change2(val, row) {
				this.$http({
					url: this.$http.adornUrl('admin/reward/saveReward'),
					method: 'post',
					params: this.$http.adornParams({
					// data: this.$http.adornData({
						'rewardProp': row.rewardProp,
						'isEnable': row.isEnable,
						'num': row.num,
						'sort': row.sort,
						'rewardId': row.rewardId,
					})
				}).then(({
					data
				}) => {
					if(data.code==0){
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					}else{
						this.$message({
							message: data.msg,
							type: 'warning',
							duration: 1500,
							onClose: () => {
								this.dataSelect()
							}
						})
					}
				})
			},
			// 添加标签
			btnTj() {
				if (this.bq == '' || this.bq == ' ') {
					this.$notify({
						title: '提示',
						duration: 1800,
						message: '请输入标签名',
						type: 'warning'
					});
					return
				} else {
					console.log('this.bq', this.bq)
					this.labels.push(this.bq)
					this.bq = ''
				}
			
			},
			// 删除标签
			dels1(index) {
				this.labels.splice(index, 1);
				console.log(this.labels)
			},
		},
		mounted() {
			this.dataSelect()
		}
	}
</script>

<style>
	.bqList {
		padding: 4px 14px;
		margin: 4px;
		border: 1px solid #efefef;
		font-size: 12px;
		color: #999;
		border-radius: 4px;
		margin-right: 15px;
	}
	
	.delss {
		display: none;
		position: relative;
	}
	
	.delss .el-icon-delete {
		position: absolute;
		top: 0;
	}
	
	.bqList:hover .delss {
		display: initial;
		opacity: 0.5;
	
	}
	
	.tj {
		padding: 6px !important;
		margin: 4px;
		font-size: 12px;
		border: 1px solid #ccc;
		border-radius: 4px;
	}
</style>
